<script setup lang="ts">
import { getConsultOrderPayUrl } from '@/services/consul'
import { showToast } from 'vant'
import { ref } from 'vue'

const props = defineProps<{
  actualPayment: number //支付金额
  orderId: string //订单id
  show: boolean //是否关闭
  onClose?: () => void //关闭前的操作
  payCallBack: string //会跳路径
}>()

// 派发事件
const emit = defineEmits<{
  (e: 'update-show', val: boolean): void
}>()
const paymentMethod = ref<1 | 0>()

// 确认支付
const Pay = async () => {
  // 仓库中数据没有才出现 --点击去支付但没有支付 此时仓库数据为空
  if (paymentMethod.value === undefined) return showToast('请选择支付方式')
  const res = await getConsultOrderPayUrl({
    orderId: props.orderId,
    paymentMethod: paymentMethod.value,
    payCallback: 'http://localhost:5173' + props.payCallBack
  })
  window.location.href = res.data.payUrl
}
</script>

<template>
  <div class="page">
    <van-action-sheet
      :before-close="onClose"
      :closeable="false"
      :show="show"
      @update:show="emit('update-show', $event)"
      title="选择支付方式"
    >
      <div class="pay-type">
        <p class="amount">￥{{ actualPayment.toFixed(2) }}</p>
        <van-cell-group>
          <!-- 默认两个支付方式都不选择 -->
          <van-cell title="微信支付" @click="paymentMethod = 0">
            <template #icon><cp-icon name="consult-wechat" /></template>
            <template #extra
              ><van-checkbox :checked="paymentMethod === 0"
            /></template>
          </van-cell>
          <van-cell title="支付宝支付" @click="paymentMethod = 1">
            <template #icon><cp-icon name="consult-alipay" /></template>
            <template #extra
              ><van-checkbox :checked="paymentMethod === 1"
            /></template>
          </van-cell>
        </van-cell-group>
        <div class="btn">
          <van-button @click="Pay" type="primary" round block
            >立即支付</van-button
          >
        </div>
      </div>
    </van-action-sheet>
  </div>
</template>

<style lang="scss" scoped>
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>
